<template>
  <div class="header-notice">
    <el-popover
      placement="bottom"
      ref="popoverRef"
      width="400"
      trigger="click">
      <div>
          <el-tabs v-model="currentTab">
              <el-tab-pane label="消息通知" name="notification" />
              <el-tab-pane label="系统消息" name="systemInfo" />
          </el-tabs>
          <div v-show="currentTab === 'notification'" style="padding: 0 10px">
            <div v-for="data in dataList" :key="data.id" class="tips">
              <span @click="detailMessage(data.id)" >{{data.contentDisplay | ellipsis}}</span>
              <span>{{getDateTime(data.sendTime)}}</span>
            </div>
            <div class="pic" v-show="dataList&& dataList.length == 0">
              <img :src="noData" alt="" style="height: 200px">
            </div>
          </div>
          <div v-show="currentTab === 'systemInfo'"  style="padding: 0 10px">  
             <div v-for="data in systemDataList" :key="data.id" class="tips">
              <span @click="detailMessage(data.id)">{{data.contentDisplay | ellipsis}}</span>
               <span>{{getDateTime(data.sendTime)}}</span>
            </div>
             <div class="pic" v-show="systemDataList && systemDataList.length == 0">
              <img :src="noData" alt="" style="height: 200px">
            </div>
          </div>
          <div class="buttonShow">
            <el-button type="text" @click="setAllRead()">全部设置已读</el-button>
            <el-button type="text"  @click="moreMessage()">查看更多>></el-button>
          </div>
      </div>
       <div class="bell-icon" slot="reference">
          <i class="el-icon-bell"/>
          <span class="red"  v-show="dataList && dataList.length > 0 || systemDataList && systemDataList.length > 0"></span>
      </div>
    </el-popover>
  </div>
</template>

<script>
import noData from '@/assets/images/noData.png'
import { readMyMessage } from '@/api/custom/message'
import { mapGetters } from 'vuex'
export default {
  name: 'HeaderNotice',
  data() {
    return {
      noData: noData,
      currentTab: "notification",
      dataList: [],
      systemDataList: []
    }
  },
   props: ['noticeUrl'],
  filters: {
    ellipsis: function(value) {
      if (!value) return "";
      if (value.length > 15) {
        return value.slice(0, 15) + "...";
      }
      return value;
    }
  },
  computed:{
    ...mapGetters({
      socketData: 'socketData'
    }),
  },
  watch: {
    socketData (newValue, old) {
      const data  = newValue && JSON.parse(newValue.data)
      if(data.hasOwnProperty('ordinaryMessage')){
        this.dataList = data.ordinaryMessage
      }
      if(data.hasOwnProperty('systemMessage')){
        this.systemDataList = data.systemMessage
      }
    }
  },
  methods: {
    setAllRead() { // 设置全部已读
      if(this.dataList.length > 0 || this.systemDataList.length > 0){
        readMyMessage([]).then((res)=>{
          this.dataList = []
          this.systemDataList = []
          this.$router.go(0);
        })
        }
      this.$refs.popoverRef.doClose()
    },
      //时间戳转日期时间
    getDateTime(time) {
        if (!time) return ''
        let date = new Date(time)
        let year = date.getFullYear()
        let month = date.getMonth() + 1
        let day = date.getDate()
        let hour = date.getHours()
        let minute = date.getMinutes()
        let second = date.getSeconds()
        return `${year}-${month<10?'0':''}${month}-${day<10?'0':''}${day} ${hour<10?'0':''}${hour}:${minute<10?'0':''}${minute}:${second<10?'0':''}${second}`
    },
    moreMessage() { // 跳转到我的消息页面
      this.$refs.popoverRef.doClose()
      // this.$router.push("/custom/message/myMessage");
      this.$router.push(this.noticeUrl);
    },
    detailMessage(row) { // 跳转到我的消息查看页面
      this.$router.push({
          path: "/message/detail",
          query: {
            id: row,
          }
        });
        this.$refs.popoverRef.doClose()
      },
  }
}
</script>

<style lang="scss" scoped>
  .pic{
    text-align: center;
  }
  .buttonShow{
    display: flex;
    justify-content: space-around;
    margin: 30px 0 10px 0
  }
  .tips{
        display: flex;
        justify-content: space-between;
        line-height: 2;
        :nth-child(1){
          cursor: pointer;
        }
    }
  .header-notice{
    font-size: 0 !important;
    .bell-icon {
      position: relative;
      i{
         cursor: pointer;
        font-size: 18px;
        vertical-align: middle;
        font-weight: 900;
      }
        .red {
          position: absolute;
          top: 15px;
          right: -2px;
          background-color: red;
          border-radius: 10px;
          height: 10px;
          line-height: 10px;
          padding: 0 4px;
          border: 1px solid #fff;
        }
    }
  }
</style>
